package dk.ucn.houseenabler;

import java.io.File;
import java.io.FileOutputStream;
import android.os.Environment;
import android.util.Log;
public class FileHandler{

   
    /**
     * Exports the data to a file. There are two types of files - .mil or .fun.
     * .fun(Personal) - Needs byte array with 14 questions.
     * .mil(Environment) - Needs byte array with 161 questions.
     * @param filetype. 0 = .fun(Personal) byte[14]. 1 = .mil(Environment) byte[161].
     */ 
    public static void exportToFile(String profileName, byte[] questions, byte filetype)
    {	    	
    	if(profileName.length() > 25 | profileName.length() <= 0)
    	{
    		//Toast.makeText(FileHandler.this, "Profile name is to long " + profileName, Toast.LENGTH_LONG).show();
    	}
    	else
    	{
    		try 
    		{
    			File sdCard = Environment.getExternalStorageDirectory();
    			File dir = new File(sdCard.getAbsolutePath() + "/Ergoterapeut");
    			
    			//If directory does not exist - make it.
    			if(!dir.exists())
    			{
    				dir.mkdir();
    			}
    			
    			//filetype - .mil(environment) or .fun filetype(personal)
    			String fileName ="";
    			switch(filetype)
    			{
    				case 0: //.fun
    				{
    					fileName = "Personal.fun";
    					createFile(dir, fileName, profileName, questions);
    					break;
    				}
    				case 1: //.mil
    				{
    					fileName = "Environment.mil";
    					createFile(dir, fileName, profileName, questions);
    					break;
    				}
    				default: // FEJL - ingen filtype er valgt!
    				{
    					Log.e("exportToFile()", "Invalid filetype");
    				}	
    			}
			} 
    		catch (Exception e) 
    		{
				Log.e("exportToFile(...)", e.getMessage());
			}	
    	} 	
    }
    private static void createFile(File directory, String fileName, String profileName, byte[] questions)
    {
    	try
    	{
	    	//This should be changed, so each file is unique from each other.
			File data = new File(directory, fileName);
			
			//true - write to the end of the file.
	        FileOutputStream fos = new FileOutputStream(data, true); 
	        
	        //length for the profile name should always be 25.
	        fos.write(profileName.length());
	        
	        //writing profileName to file.
	        fos.write(profileName.getBytes());
	        
	        //profileName must be exact 25 chars.
	        if(profileName.length() < 25)
	        {
	            int charsMissing = 25 - profileName.length();
	            byte[] chars = new byte[charsMissing];
	            //default value is 0 in a byte array. We don't need to fill it with 0's.
	
	            fos.write(chars);
	        }
	        
	        //questions
	        fos.write(questions);
	        
	        //An extra array need to be added to the file if the type is .fun.
	        if(fileName.equals("Personal.fun"))
	        {
	        	byte[] extra = new byte[128];
	        	
	        	for(int i = 0; i < extra.length; i+=4)
	        		if(i >= 64)
	        		{
	        			extra[i] = 1;
	        		}	        	
	        	fos.write(extra);
	        }
	    
	        fos.flush();
	        fos.close();
    	}
    	catch(Exception e)
    	{
    		Log.e("createFile()", e.getMessage());
    	}
    }
}